package com.hydra.auth.service.impl;

import com.hydra.auth.feign.OpenFeignMailService;
import com.hydra.base.common.Constant;
import com.hydra.base.utils.ValidateUtil;
import com.hydra.mail.services.MailService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class MailValidateCodeServiceImpl extends ValidateCodeServiceImpl {

    /**邮件服务*/
    @Resource
    private OpenFeignMailService openFeignMailService;

    /**
     * 校验格式
     */
    @Override
    protected void validate(String target) {
        ValidateUtil.validateEmail(target, "邮箱", false);
    }

    @Override
    protected String getPrefix() {
        return Constant.REDIS_KEY_VALIDATE_CODE_PREFIX_MAIL;
    }

    /**
     * 保存的redis时效(秒)
     */
    @Override
    protected long getSeconds() {
        return 30 * 60;
    }

    /**
     * 真正的发送方法
     */
    @Override
    protected boolean send(String target, String code) {
        MailService.sendValidateCode.Input input = new MailService.sendValidateCode.Input();
        input.setSendTo(target);
        input.setValidateCode(code);
        String time = getSeconds()/60 + "分钟";
        input.setInvalidTime(time);
        openFeignMailService.sendValidateCode(input);
        return true;
    }
}
